package com.hzj.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzj.recipe.entity.Ingredient;
import com.hzj.recipe.entity.RecipeIngredient;
import com.hzj.recipe.entity.RecipeIngredient;
import com.hzj.recipe.mapper.RecipeIngredientMapper;
import com.hzj.recipe.service.RecipeIngredientService;
import com.hzj.recipe.mapper.RecipeIngredientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 蜗牛菌
* @description 针对表【recipe_ingredient(食谱-食材关联表)】的数据库操作Service实现
* @createDate 2024-01-04 10:49:47
*/
@Service
public class RecipeIngredientServiceImpl extends ServiceImpl<RecipeIngredientMapper, RecipeIngredient>
    implements RecipeIngredientService{

    @Autowired
    private RecipeIngredientMapper recipeIngredientMapper;
    
    @Override
    public Object listAll(Integer index, Integer size, RecipeIngredient recipeIngredient) {
        //默认第一页开始，每页5条
        if(index == null ||index==0){
            index=1;
        }
        if(size==null||size==0){
            size=5;
        }
        Page<RecipeIngredient> pageInfo = new Page(index, size);
        LambdaQueryWrapper<RecipeIngredient> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (recipeIngredient.getRelationId() != null && recipeIngredient.getRelationId() != 0 && !Objects.equals(recipeIngredient.getRelationId(), "")) {
            lambdaQueryWrapper.eq(RecipeIngredient::getRelationId, recipeIngredient.getRelationId());
        }
        if (recipeIngredient.getRecipeId() != null && recipeIngredient.getRecipeId() != 0 && !Objects.equals(recipeIngredient.getRecipeId(), "")) {
            lambdaQueryWrapper.eq(RecipeIngredient::getRecipeId, recipeIngredient.getRecipeId());
        }
        if (recipeIngredient.getIngredientId() != null && recipeIngredient.getIngredientId() != 0 && !Objects.equals(recipeIngredient.getIngredientId(), "")) {
            lambdaQueryWrapper.eq(RecipeIngredient::getIngredientId, recipeIngredient.getIngredientId());
        }

        Page<RecipeIngredient> recipeIngredientPage = recipeIngredientMapper.selectPage(pageInfo, lambdaQueryWrapper);
        return recipeIngredientPage;
    }
}




